﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using ebird.CaptchaGenerator;
public partial class Captchatest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["captureImage"] = GenerateRandomString();
            imgCaptchaImage.ImageUrl = "Imagegeneration.aspx";
            CreateCAPTCHAImage(this.Session["captureImage"].ToString(), 140, 30, "Verdana");
        }

    }
    protected void rr_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && (txtTuring.Text.ToString() == Session["randomStr"].ToString()))
        {
            // The Code to insert data
        }
        else
        {
            ln.Text = "Please enter info correctly";
        }
    }
    public string GenerateRandomString()
    {
        string rndResult = "";
        StringBuilder randomText = new StringBuilder();

        if (Session["Code"] == null)
        {
            string alphabets = "abcdefghijklmnopqrstuvwxyz1234567890";

            Random r = new Random();
            for (int j = 0; j <= 5; j++)
            {

                randomText.Append(alphabets[r.Next(alphabets.Length)]);
            }

            rndResult = randomText.ToString();
        }

        return rndResult;
    }
    private void CreateCAPTCHAImage(string text, int width, int height, string fontFamily)
    {
        HttpContext objHttpContext = HttpContext.Current;
        // Create a new 32-bit bitmap image.
        Bitmap objBitmap = new Bitmap(width, height,
            System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        // Create a graphics objHttpContextect for drawing.
        Graphics objGraphics = Graphics.FromImage(objBitmap);
        Rectangle objRectangle = new Rectangle(0, 0, width, height);
       // objGraphics.SmoothingMode = System.Drawing.Ima.SmoothingMode.AntiAlias;

        // Fill background
        using (SolidBrush objSolidBrush = new SolidBrush(Color.LightGray))
        {
            objGraphics.FillRectangle(objSolidBrush, objRectangle);
        }

        // Set up the text font.
        //int emSize = (int)(width * 2 / text.Length);
        Font objFont = new Font(fontFamily, 12);

        // Adjust the font size until
        // the text fits within the image.
        SizeF objSizeF = new SizeF(0, 0);
        SizeF objSize = new SizeF(width, height);

        //create graphics class objects.
        GraphicsPath objGraphicsPath = new GraphicsPath();
        StringFormat objStringFormat = new StringFormat();
        objStringFormat.Alignment = StringAlignment.Center;
        objStringFormat.LineAlignment = StringAlignment.Center;
        objGraphicsPath.AddString(text, objFont.FontFamily,
                (int)objFont.Style, objFont.Size, objRectangle, objStringFormat);

        //Create the solid brush for background
        SolidBrush objSolidBrushColor = new SolidBrush(Color.Black);
        objGraphics.FillPath(objSolidBrushColor, objGraphicsPath);

        #region Distortion
        // Iterate over every pixel
        double distort = new Random().Next(5, 10) * (new Random().Next(5) == 1 ? 1 : -1);

        // Copy the image so that we're always using the original for
        // source color
        using (Bitmap copy = (Bitmap)objBitmap.Clone())
        {
            for (int waveY = 0; waveY < height; waveY++)
            {
                for (int waveX = 0; waveX < width; waveX++)
                {
                    // Adds a simple wave

                    int newX =
                      (int)(waveX + (distort * Math.Sin(Math.PI * waveY / 54.0)));
                    int newY =
                      (int)(waveY + (distort * Math.Cos(Math.PI * waveX / 44.0)));
                    if (newX < 0 || newX >= width)
                        newX = 0;
                    if (newY < 0 || newY >= height)
                        newY = 0;
                    objBitmap.SetPixel(waveX, waveY,
                    copy.GetPixel(newX, newY));
                }

            }
        }
        #endregion Distortion

//        objHttpContext.Response.ContentType = image;
        objBitmap.Save(this.Response.OutputStream, ImageFormat.Jpeg);
    }
}
